﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace EasyFTimeSync
{
    public partial class MainF : Form
    {
        string SourceFolder = "";
        string FileFolder = "";
        public MainF()
        {
            InitializeComponent();
            txt_Details.Text = "简易制作，不加错误判断，请规定操作！\n支持子目录！";
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {

            if (fbd_OpenFolder.ShowDialog() == DialogResult.OK)
            {
                SourceFolder = fbd_OpenFolder.SelectedPath;
                if (!SourceFolder.EndsWith("\\"))
                {
                    SourceFolder = SourceFolder + "\\";
                }
                txt_SourceFolder.Text = SourceFolder.ToString();
            }
        }

        private void btn_Open2_Click(object sender, EventArgs e)
        {
            if (fbd_OpenFolder.ShowDialog() == DialogResult.OK)
            {
                FileFolder = fbd_OpenFolder.SelectedPath;
                if (!FileFolder.EndsWith("\\"))
                {
                    FileFolder = FileFolder + "\\";
                }
                txt_FileFolder.Text = FileFolder.ToString();
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            txt_Details.Text = "";
            ChangeTime(SourceFolder, FileFolder);


        }

        private void ChangeTime(string SourceFolder,string FileFolder)
        {
            string sf = SourceFolder, ff = FileFolder;
            if (sf != "" && ff != "" && sf != ff)
            {
                DirectoryInfo d = new DirectoryInfo(sf);
                //FileSystemInfo[] fs= d.GetFileSystemInfos();
                FileSystemInfo[] fs = d.GetFileSystemInfos("*.*");
                //返回当前文件夹下的第一层子文件和子文件夹
                string file;
                foreach (FileSystemInfo f in fs)
                {
                    file = f.ToString();
                    if (f is DirectoryInfo)
                    {
                        ChangeTime(sf + file + "\\", ff + file + "\\");
                    }
                    else
                    {
                        
                        if(File.Exists(ff+file))
                        {
                        // 读取文件的创建、修改、访问时间
                            FileInfo fi = new FileInfo(sf + file);
                        //Console.WriteLine(fi.CreationTime.ToString());
                        //Console.WriteLine(fi.LastWriteTime.ToString());
                        //Console.WriteLine(fi.LastAccessTime.ToString());

                        // 改变（设置）文件的创建、修改、访问时间
                            if(cb_CreateTime.Checked==true)
                                File.SetCreationTime(ff + file, fi.CreationTime);
                            if (cb_LastWriteTime.Checked == true)
                                File.SetLastWriteTime(ff + file, fi.LastWriteTime);
                            if (cb_LastAccessTime.Checked == true)
                                File.SetLastAccessTime(ff + file, fi.LastAccessTime);
                            
                            txt_Details.AppendText(f + "已经修改\n");
                        }
                    }
                }
            }
            else
            {
                txt_Details.Text = "目录错误！";
            }
        }
    }
}
